package cn.flyingocean;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;


public class StockInfo implements FileHandler,StockSorter {
	private String DATE;
	private String LASTUPDATE;
	private String ID;
	private String TITLE;
	private String AUTHOR;
	private String CONTENT;
	private String ANSWERAUTHOR; 
	private String ANSWER;
	/**
	 * 	ʵ�ֽӿ� FileHandler
	 */
	public void pullFromFile(ArrayList<StockInfo> siArray) {
		try {
			FileInputStream file_fi = new FileInputStream("sad.txt");
			InputStreamReader file_sr= new InputStreamReader(file_fi,"UTF-8"); 
			BufferedReader file = new BufferedReader(file_sr);
			boolean eof = false;

			while(!eof) {
				StockInfo temp = new StockInfo();
				String line = file.readLine();
				if(line == null) 
					{
						eof = true;
						System.out.println("here!");
					}
				else {
					String[] data = line.split("\t");
					System.out.print(data[0]);
					temp.setID(data[0]);
					temp.setTITLE(data[1]);
					temp.setAUTHOR(data[2]);
					temp.setDATE(data[3]);
					temp.setLASTUPDATE(data[4]);
					temp.setCONTENT(data[5]);
					temp.setANSWERAUTHOR(data[6]);
					temp.setANSWER(data[7]);
					siArray.add(temp);
				}
			}
			
			file.close();
		}
		catch(IOException e) {
			System.out.println("Error: "+e.getMessage());
		}
	}
	public  int pushIntoFile(ArrayList<StockInfo> siArray)
	{
		try {
			FileOutputStream file_os = new FileOutputStream("sad.txt");
			OutputStreamWriter file_sw= new OutputStreamWriter(file_os,"UTF-8"); 
			BufferedWriter file = new BufferedWriter(file_sw);
			int i ;
			for( i = 0;i<siArray.size();i++)
			{
				file.write( siArray.get(i).getID() );
				file.write("\t");
				file.write( siArray.get(i).getTITLE() );
				file.write("\t");
				file.write( siArray.get(i).getAUTHOR() );
				file.write("\t");
				file.write( siArray.get(i).getDATE() );
				file.write("\t");
				file.write( siArray.get(i).getLASTUPDATE() );
				file.write("\t");
				file.write( siArray.get(i).getCONTENT() );
				file.write("\t");
				file.write( siArray.get(i).getANSWERAUTHOR() );
				file.write("\t");
				file.write( siArray.get(i).getANSWER() );
				file.newLine();
			}
			file.close();
			return  i-1;
		}
		catch(IOException e) {
			System.out.println("Error: "+e.getMessage());
		}
		return -1;
	}
	/**
	 * ʵ�ֽӿ�  StockSorter
	 */
	public void ascending(ArrayList<StockInfo> siArray)
	{
		for(int i=0;i<siArray.size();i++)
		{
			for(int j=0;j<siArray.size()-1-i;j++)
			{
				if( siArray.get(j).getANSWER().length() > siArray.get(j+1).getANSWER().length() ) {
					StockInfo temp;
					temp = siArray.get(j);	
					siArray.set(j, siArray.get(j+1));
					siArray.set(j+1, temp);
				}
			}
		}
	}
	public void descending(ArrayList<StockInfo> siArray)
	{
		for(int i=0;i<siArray.size();i++)
		{
			for(int j=0;j<siArray.size()-i-1;j++)
			{
				if( siArray.get(j).getANSWER().length() < siArray.get(j+1).getANSWER().length() ) {
					StockInfo temp;
					temp = siArray.get(j);	
					siArray.set(j, siArray.get(j+1));
					siArray.set(j+1, temp);
				}
			}
		}
	}
	
	/**
	 * ��Ա������get,set����
	 * @return
	 */	public String getID() {
		return ID;
	}
	public void setID(String iD) {
		ID = iD;
	}
	public String getTITLE() {
		return TITLE;
	}
	public void setTITLE(String tITLE) {
		TITLE = tITLE;
	}
	public String getAUTHOR() {
		return AUTHOR;
	}
	public void setAUTHOR(String aUTHOR) {
		AUTHOR = aUTHOR;
	}
	public String getDATE() {
		return DATE;
	}
	public void setDATE(String dATE) {
		DATE = dATE;
	}
	public String getLASTUPDATE() {
		return LASTUPDATE;
	}
	public void setLASTUPDATE(String lASTUPDATE) {
		LASTUPDATE = lASTUPDATE;
	}
	public String getCONTENT() {
		return CONTENT;
	}
	public void setCONTENT(String cONTENT) {
		CONTENT = cONTENT;
	}
	public String getANSWERAUTHOR() {
		return ANSWERAUTHOR;
	}
	public void setANSWERAUTHOR(String aNSWERAUTHOR) {
		ANSWERAUTHOR = aNSWERAUTHOR;
	}
	public String getANSWER() {
		return ANSWER;
	}
	public void setANSWER(String aNSWER) {
		ANSWER = aNSWER;
	} 
}
